package co.baiku.boot.config;

import co.baiku.boot.common.tools.StringTools;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 公众号配置
 */
@ConditionalOnProperty("ajavaer.weixin.public.appid")
@ConfigurationProperties(prefix = "ajavaer.weixin.public")
public class WeixinPublicConfig implements InitializingBean {
    private String appid;
    private String secret;
    /**
     * 服务器配置token
     * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1472017492_58YV5
     */
    private String token;

    private String accessTokenCacheKey = "WEIXIN_PUBLIC_ACCESSTOKEN_CACHE";
    private String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";

    /* ================================用户相关============================================*/
    /**
     * @see <a href='https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183'>获取用户基本信息(UnionID机制)</a>
     */
    private String user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN";
    /**
     * @see <a href='https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183'>批量获取用户基本信息</a>
     */
    private String user_info_batchget_url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={0}";
    /**
     * @see <a href='https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183'>获取用户列表</a>
     */
    private String user_get_url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}&next_openid={1}";

    /*=================================菜单相关=============================================*/
    /**
     * 自定义菜单查询接口(get方式)
     *
     * @see <a href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141014">自定义菜单查询接口</a>
     */
    private String get_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}";
    /**
     * 自定义菜单创建接口(post方式)
     *
     * @see <a href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013">自定义菜单创建接口</a>
     */
    private String create_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}";
    /**
     * 自定义菜单删除接口(get方式)
     *
     * @see <a href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141015">自定义菜单删除接口</a>
     */
    private String delete_menu_url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={0}";
    /**
     * 创建个性化菜单(post方式)
     *
     * @see <a href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296">创建个性化菜单</a>
     */
    private String add_conditional_url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token={0}";
    /**
     * 删除个性化菜单(post方式)
     *
     * @see <a href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296">删除个性化菜单</a>
     */
    private String del_conditional_url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token={0}";
    /**
     * 测试个性化菜单匹配结果(post方式)
     *
     * @see <a href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296">测试个性化菜单匹配结果</a>
     */
    private String menu_trymatch_url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token={0}";
    /**
     * 发送模版消息(post方式)
     *
     * @see <a href="https://mp.weixin.qq.com/advanced/tmplmsg?action=faq&token=1572195435&lang=zh_CN">模版消息</a>
     */
    private String template_send_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}";

    @Override
    public void afterPropertiesSet() throws Exception {
        this.access_token_url = String.format(this.access_token_url, this.appid, this.secret);
    }

    public String getUser_info_url(String access_token, String openId) {
        return StringTools.format(user_info_url, access_token, openId);
    }

    public String getUser_get_url(String access_token, String nextOpenId) {
        return StringTools.format(user_get_url, access_token, nextOpenId);
    }

    public String getUser_info_batchget_url(String access_token) {
        return StringTools.format(user_info_batchget_url, access_token);
    }

    public String getTemplate_send_url(String access_token) {
        return StringTools.format(template_send_url, access_token);
    }

    public String getGet_menu_url(String access_token) {
        return StringTools.format(get_menu_url, access_token);
    }

    public String getCreate_menu_url(String access_token) {
        return StringTools.format(create_menu_url, access_token);
    }

    public String getDelete_menu_url(String access_token) {
        return StringTools.format(delete_menu_url, access_token);
    }

    public String getAdd_conditional_url(String access_token) {
        return StringTools.format(add_conditional_url, access_token);
    }

    public String getDel_conditional_url(String access_token) {
        return StringTools.format(del_conditional_url, access_token);
    }

    public String getMenu_trymatch_url(String access_token) {
        return StringTools.format(menu_trymatch_url, access_token);
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getAccessTokenCacheKey() {
        return accessTokenCacheKey;
    }

    public void setAccessTokenCacheKey(String accessTokenCacheKey) {
        this.accessTokenCacheKey = accessTokenCacheKey;
    }

    public String getAccess_token_url() {
        return StringTools.format(access_token_url, appid, secret);
    }

    public void setAccess_token_url(String access_token_url) {
        this.access_token_url = access_token_url;
    }

    public void setUser_info_url(String user_info_url) {
        this.user_info_url = user_info_url;
    }

    public void setUser_get_url(String user_get_url) {
        this.user_get_url = user_get_url;
    }

    public void setUser_info_batchget_url(String user_info_batchget_url) {
        this.user_info_batchget_url = user_info_batchget_url;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

}
